home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 320 / compsrc2 / jump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  39.7 KB  |  1,453 lines

  1. /* Optimize jump instructions, for GNU compiler.
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. /* This is the jump-optimization pass of the compiler.
  23.    It is run two or three times: once before cse, sometimes once after cse,
  24.    and once after reload (before final).
  25.  
  26.    jump_optimize deletes unreachable code and labels that are not used.
  27.    It also deletes jumps that jump to the following insn,
  28.    and simplifies jumps around unconditional jumps and jumps
  29.    to unconditional jumps.
  30.  
  31.    Each CODE_LABEL has a count of the times it is used
  32.    stored in the LABEL_NUSES internal field, and each JUMP_INSN
  33.    has one label that it refers to stored in the
  34.    JUMP_LABEL internal field.  With this we can detect labels that
  35.    become unused because of the deletion of all the jumps that
  36.    formerly used them.  The JUMP_LABEL info is sometimes looked
  37.    at by later passes.
  38.  
  39.    Optionally, cross-jumping can be done.  Currently it is done
  40.    only the last time (when after reload and before final).
  41.    In fact, the code for cross-jumping now assumes that register
  42.    allocation has been done, since it uses `rtx_renumbered_equal_p'.
  43.  
  44.    Jump optimization is done after cse when cse's constant-propagation
  45.    causes jumps to become unconditional or to be deleted.
  46.  
  47.    Unreachable loops are not detected here, because the labels
  48.    have references and the insns appear reachable from the labels.
  49.    find_basic_blocks in flow.c finds and deletes such loops.
  50.  
  51.    The subroutines delete_insn, redirect_jump, invert_jump, next_real_insn
  52.    and prev_real_insn are used from other passes as well.  */
  53.  
  54. #include "config.h"
  55. #include "rtl.h"
  56. #include "flags.h"
  57. #include "regs.h"
  58.  
  59. /* ??? Eventually must record somehow the labels used by jumps
  60.    from nested functions.  */
  61. /* Pre-record the next or previous real insn for each label?
  62.    No, this pass is very fast anyway.  */
  63. /* Condense consecutive labels?
  64.    This would make life analysis faster, maybe.  */
  65. /* Optimize jump y; x: ... y: jumpif... x?
  66.    Don't know if it is worth bothering with.  */
  67. /* Optimize two cases of conditional jump to conditional jump?
  68.    This can never delete any instruction or make anything dead,
  69.    or even change what is live at any point.
  70.    So perhaps let combiner do it.  */
  71.  
  72. /* Vector indexed by uid.
  73.    For each CODE_LABEL, index by its uid to get first unconditional jump
  74.    that jumps to the label.
  75.    For each JUMP_INSN, index by its uid to get the next unconditional jump
  76.    that jumps to the same label.
  77.    Element 0 is the start of a chain of all return insns.
  78.    (It is safe to use element 0 because insn uid 0 is not used.  */
  79.  
  80. rtx *jump_chain;
  81.  
  82. rtx delete_insn ();
  83. void redirect_jump ();
  84. void invert_jump ();
  85. rtx next_real_insn ();
  86. rtx prev_real_insn ();
  87. rtx next_label ();
  88.  
  89. static void mark_jump_label ();
  90. static void delete_jump ();
  91. static void invert_exp ();
  92. static void redirect_exp ();
  93. static rtx follow_jumps ();
  94. static int tension_vector_labels ();
  95. static void find_cross_jump ();
  96. static void do_cross_jump ();
  97. static enum rtx_code reverse_condition ();
  98. static int jump_back_p ();
  99. static int condjump_p ();
  100.  
  101. /* Delete no-op jumps and optimize jumps to jumps
  102.    and jumps around jumps.
  103.    Delete unused labels and unreachable code.
  104.    If CROSS_JUMP is nonzero, detect matching code
  105.    before a jump and its destination and unify them.
  106.    If NOOP_MOVES is nonzero, also delete no-op move insns
  107.    and perform machine-specific peephole optimizations
  108.    (but flag_no_peephole inhibits the latter).
  109.  
  110.    If `optimize' is zero, don't change any code,
  111.    just determine whether control drops off the end of the function.
  112.    This case occurs when we have -W and not -O.
  113.    It works because `delete_insn' checks the value of `optimize'
  114.    and refrains from actually deleting when that is 0.  */
  115.  
  116. void
  117. jump_optimize (f, cross_jump, noop_moves)
  118.      rtx f;
  119. {
  120.   register rtx insn;
  121.   int changed;
  122.   int first = 1;
  123.   int max_uid = 0;
  124.   rtx last_insn;
  125.  
  126.   /* Initialize LABEL_NUSES and JUMP_LABEL fields.  */
  127.  
  128.   for (insn = f; insn; insn = NEXT_INSN (insn))
  129.     {
  130.       if (GET_CODE (insn) == CODE_LABEL)
  131.     LABEL_NUSES (insn) = 0;
  132.       if (GET_CODE (insn) == JUMP_INSN)
  133.     JUMP_LABEL (insn) = 0;
  134.       if (INSN_UID (insn) > max_uid)
  135.     max_uid = INSN_UID (insn);
  136.     }
  137.  
  138.   max_uid++;
  139.  
  140.   jump_chain = (rtx *) alloca (max_uid * sizeof (rtx));
  141.   bzero (jump_chain, max_uid * sizeof (rtx));
  142.  
  143.   /* Delete insns following barriers, up to next label.  */
  144.  
  145.   for (insn = f; insn;)
  146.     {
  147.       if (GET_CODE (insn) == BARRIER)
  148.     {
  149.       insn = NEXT_INSN (insn);
  150.       while (insn != 0 && GET_CODE (insn) != CODE_LABEL)
  151.         {
  152.           if (GET_CODE (insn) == NOTE
  153.           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
  154.         insn = NEXT_INSN (insn);
  155.           else
  156.         insn = delete_insn (insn);
  157.         }
  158.       /* INSN is now the code_label.  */
  159.     }
  160.       else
  161.     insn = NEXT_INSN (insn);
  162.     }
  163.  
  164.   /* Mark the label each jump jumps to.
  165.      Combine consecutive labels, and count uses of labels.
  166.  
  167.      For each label, make a chain (using `jump_chain')
  168.      of all the *unconditional* jumps that jump to it;
  169.      also make a chain of all returns.  */
  170.  
  171.   for (insn = f; insn; insn = NEXT_INSN (insn))
  172.     if (GET_CODE (insn) == JUMP_INSN && !insn->volatil)
  173.       {
  174.     mark_jump_label (PATTERN (insn), insn, cross_jump);
  175.     if (JUMP_LABEL (insn) != 0 && simplejump_p (insn))
  176.       {
  177.         jump_chain[INSN_UID (insn)]
  178.           = jump_chain[INSN_UID (JUMP_LABEL (insn))];
  179.         jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
  180.       }
  181.     if (GET_CODE (PATTERN (insn)) == RETURN)
  182.       {
  183.         jump_chain[INSN_UID (insn)] = jump_chain[0];
  184.         jump_chain[0] = insn;
  185.       }
  186.       }
  187.  
  188.   /* Delete all labels already not referenced.
  189.      Also find the last insn.  */
  190.  
  191.   last_insn = 0;
  192.   for (insn = f; insn; )
  193.     {
  194.       if (GET_CODE (insn) == CODE_LABEL && LABEL_NUSES (insn) == 0)
  195.     insn = delete_insn (insn);
  196.       else
  197.     {
  198.       last_insn = insn;
  199.       insn = NEXT_INSN (insn);
  200.     }
  201.     }
  202.  
  203.   if (!optimize)
  204.     {
  205.       /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
  206.      If so record that this function can drop off the end.  */
  207.  
  208.       insn = last_insn;
  209.       while (insn && (GET_CODE (insn) == CODE_LABEL
  210.               /* If machine uses explicit RETURN insns, no epilogue,
  211.              then this note precedes the drop-through RETURN.  */
  212.               || (GET_CODE (insn) == JUMP_INSN
  213.               && GET_CODE (PATTERN (insn)) == RETURN)))
  214.     insn = PREV_INSN (insn);
  215.  
  216.       if (GET_CODE (insn) == NOTE
  217.       && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END
  218.       && ! insn->volatil)
  219.     {
  220.       extern int current_function_returns_null;
  221.       current_function_returns_null = 1;
  222.     }
  223.       /* Zero the "deleted" flag of all the "deleted" insns.  */
  224.       for (insn = f; insn; insn = NEXT_INSN (insn))
  225.     insn->volatil = 0;
  226.       return;
  227.     }
  228.  
  229. #if 0
  230. #ifdef EXIT_IGNORE_STACK
  231.   /* If the last insn just adjusts the stack,
  232.      we can delete it on certain machines,
  233.      provided we have a frame pointer.  */
  234.  
  235.   if (frame_pointer_needed && EXIT_IGNORE_STACK)
  236.     {
  237.       insn = last_insn;
  238.       while (insn)
  239.     {
  240.       rtx prev;
  241.       /* Back up to a real insn.  */
  242.       if (GET_CODE (insn) != INSN && GET_CODE (insn) != JUMP_INSN
  243.           && GET_CODE (insn) != CALL_INSN)
  244.         insn = prev_real_insn (insn);
  245.       if (insn == 0)
  246.         break;
  247.       prev = PREV_INSN (insn);
  248.       /* If this insn is a stack adjust, delete it.  */
  249.       if (GET_CODE (insn) == INSN
  250.           && GET_CODE (PATTERN (insn)) == SET
  251.           && GET_CODE (SET_DEST (PATTERN (insn))) == REG
  252.           && REGNO (SET_DEST (PATTERN (insn))) == STACK_POINTER_REGNUM)
  253.         {
  254.           delete_insn (insn);
  255.           if (insn == last_insn)
  256.         last_insn = prev;
  257.         }
  258.       else
  259.         /* If we find an insn that isn't a stack adjust, stop deleting.  */
  260.         break;
  261.       /* Back up to insn before the deleted one and try to delete more.  */
  262.       insn = prev;
  263.     }
  264.     }
  265. #endif
  266. #endif
  267.  
  268.   if (noop_moves)
  269.     for (insn = f; insn; )
  270.       {
  271.     register rtx next = NEXT_INSN (insn);
  272.  
  273.     if (GET_CODE (insn) == INSN)
  274.       {
  275.         register rtx body = PATTERN (insn);
  276.  
  277.         /* Delete insns that existed just to advise flow-analysis.  */
  278.  
  279.         if (GET_CODE (body) == USE
  280.         || GET_CODE (body) == CLOBBER)
  281.           delete_insn (insn);
  282.  
  283.         /* Detect and delete no-op move instructions
  284.            resulting from not allocating a parameter in a register.  */
  285.  
  286.         else if (GET_CODE (body) == SET
  287.              && (SET_DEST (body) == SET_SRC (body)
  288.              || (GET_CODE (SET_DEST (body)) == MEM
  289.                  && GET_CODE (SET_SRC (body)) == MEM
  290.                  && rtx_equal_p (SET_SRC (body), SET_DEST (body))))
  291.              && ! SET_DEST (body)->volatil
  292.              && ! SET_SRC (body)->volatil)
  293.           delete_insn (insn);
  294.  
  295.         /* Detect and ignore no-op move instructions
  296.            resulting from smart or fortuitous register allocation.  */
  297.  
  298.         else if (GET_CODE (body) == SET)
  299.           {
  300.         int sreg = true_regnum (SET_SRC (body));
  301.         int dreg = true_regnum (SET_DEST (body));
  302.  
  303.         if (sreg == dreg && sreg >= 0)
  304.           delete_insn (insn);
  305.         else if (sreg >= 0 && dreg >= 0)
  306.           {
  307.             rtx tem = find_equiv_reg (0, insn, 0,
  308.                           sreg, 0, dreg,
  309.                           GET_MODE (SET_SRC (body)));
  310.             
  311. #ifdef PRESERVE_DEATH_INFO_REGNO_P
  312.             /* Deleting insn could lose a death-note for SREG or DREG
  313.                so don't do it if final needs accurate death-notes.  */
  314.             if (! PRESERVE_DEATH_INFO_REGNO_P (dreg)
  315.             && ! PRESERVE_DEATH_INFO_REGNO_P (dreg))
  316. #endif
  317.               if (tem != 0
  318.               && GET_MODE (tem) == GET_MODE (SET_DEST (body)))
  319.             delete_insn (insn);
  320.           }
  321.           }
  322.       }
  323.       insn = next;
  324.     }
  325.  
  326.   /* Now iterate optimizing jumps until nothing changes over one pass.  */
  327.   changed = 1;
  328.   while (changed)
  329.     {
  330.       register rtx next;
  331.       changed = 0;
  332.  
  333.       for (insn = f; insn; insn = next)
  334.     {
  335.       next = NEXT_INSN (insn);
  336.  
  337.       /* On the first iteration, if this is the last jump pass
  338.          (just before final), do the special peephole optimizations.  */
  339.  
  340.       if (noop_moves && first && !flag_no_peephole)
  341.         if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
  342.           peephole (insn);
  343.  
  344.       /* Tension the labels in dispatch tables.  */
  345.  
  346.       if (GET_CODE (insn) == JUMP_INSN)
  347.         {
  348.           if (GET_CODE (PATTERN (insn)) == ADDR_VEC)
  349.         changed |= tension_vector_labels (PATTERN (insn), 0, noop_moves);
  350.           if (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
  351.         changed |= tension_vector_labels (PATTERN (insn), 1, noop_moves);
  352.         }
  353.  
  354.       if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
  355.         {
  356.           register rtx reallabelprev = prev_real_insn (JUMP_LABEL (insn));
  357.  
  358.           /* Delete insns that adjust stack pointer before a return,
  359.          if this is the last jump-optimization before final
  360.          and we need to have a frame pointer.  */
  361. #if 0
  362. #ifdef EXIT_IGNORE_STACK
  363.           if (noop_moves && frame_pointer_needed && EXIT_IGNORE_STACK
  364.           && NEXT_INSN (JUMP_LABEL (insn)) == 0)
  365.         {
  366.           rtx prev = prev_real_insn (insn);
  367.           if (prev != 0
  368.               && GET_CODE (prev) == INSN
  369.               && GET_CODE (PATTERN (prev)) == SET
  370.               && GET_CODE (SET_DEST (PATTERN (prev))) == REG
  371.               && REGNO (SET_DEST (PATTERN (prev))) == STACK_POINTER_REGNUM)
  372.             {
  373.               delete_insn (prev);
  374.               changed = 1;
  375.             }
  376.         }
  377. #endif
  378. #endif
  379.  
  380.           /* Detect jump to following insn.  */
  381.           if (reallabelprev == insn && condjump_p (insn))
  382.         {
  383.           reallabelprev = PREV_INSN (insn);
  384.           delete_jump (insn);
  385.           changed = 1;
  386.         }
  387.           /* Detect jumping over an unconditional jump.  */
  388.           else if (reallabelprev != 0
  389.                && GET_CODE (reallabelprev) == JUMP_INSN
  390.                && prev_real_insn (reallabelprev) == insn
  391.                && no_labels_between_p (insn, reallabelprev)
  392.                && simplejump_p (reallabelprev)
  393.                /* Ignore this if INSN is a hairy kind of jump,
  394.               since they may not be invertible.
  395.               This is conservative; could instead construct
  396.               the inverted insn and try recognizing it.  */
  397.                && condjump_p (insn))
  398.         {
  399.           /* Delete the original unconditional jump (and barrier).  */
  400.           /* But don't let its destination go with it.  */
  401.           ++LABEL_NUSES (JUMP_LABEL (reallabelprev));
  402.           delete_insn (reallabelprev);
  403.           /* Now change the condition, and make it go to the
  404.              place the deleted jump went to.
  405.              This may cause the label after the deletion to go away.
  406.              But now that the unconditional jump and its barrier
  407.              are gone, that is ok.  */
  408.           invert_jump (insn, JUMP_LABEL (reallabelprev));
  409.           --LABEL_NUSES (JUMP_LABEL (reallabelprev));
  410.           next = insn;
  411.           changed = 1;
  412.         }
  413.           else
  414.         {
  415.           /* Detect a jump to a jump.  */
  416.           {
  417.             register rtx nlabel
  418.               = follow_jumps (JUMP_LABEL (insn), noop_moves);
  419.             if (nlabel != JUMP_LABEL (insn))
  420.               {
  421.             redirect_jump (insn, nlabel);
  422.             changed = 1;
  423.             next = insn;
  424.               }
  425.           }
  426.  
  427.           /* Look for   if (foo) bar; else break;  */
  428.           /* The insns look like this:
  429.              insn = condjump label1;
  430.                 ...range1 (some insns)...
  431.             jump label2;
  432.              label1:
  433.                 ...range2 (some insns)...
  434.             jump somewhere unconditionally
  435.              label2:  */
  436.           {
  437.             rtx label1 = next_label (insn);
  438.             rtx range1end = label1 ? prev_real_insn (label1) : 0;
  439.             /* Don't do this optimization on the first round, so that
  440.                jump-around-a-jump gets simplified before we ask here
  441.                whether a jump is unconditional.  */
  442.             if (! first
  443.             && JUMP_LABEL (insn) == label1
  444.             && LABEL_NUSES (label1) == 1
  445.             && GET_CODE (range1end) == JUMP_INSN
  446.             && simplejump_p (range1end))
  447.               {
  448.             rtx label2 = next_label (label1);
  449.             rtx range2end = label2 ? prev_real_insn (label2) : 0;
  450.             if (range1end != range2end
  451.                 && JUMP_LABEL (range1end) == label2
  452.                 && GET_CODE (range2end) == JUMP_INSN
  453.                 && GET_CODE (NEXT_INSN (range2end)) == BARRIER)
  454.               {
  455.                 rtx range1beg = NEXT_INSN (insn);
  456.                 rtx range2beg = NEXT_INSN (label1);
  457.                 rtx range1after = NEXT_INSN (range1end);
  458.                 rtx range2after = NEXT_INSN (range2end);
  459.                 /* Splice range2 between INSN and LABEL1.  */
  460.                 NEXT_INSN (insn) = range2beg;
  461.                 PREV_INSN (range2beg) = insn;
  462.                 NEXT_INSN (range2end) = range1after;
  463.                 PREV_INSN (range1after) = range2end;
  464.                 /* Splice range1 between LABEL1 and LABEL2.  */
  465.                 NEXT_INSN (label1) = range1beg;
  466.                 PREV_INSN (range1beg) = label1;
  467.                 NEXT_INSN (range1end) = range2after;
  468.                 PREV_INSN (range2after) = range1end;
  469.                 /* Invert the jump condition, so we
  470.                    still execute the same insns in each case.  */
  471.                 invert_jump (insn, label1);
  472.                 changed = 1;
  473.                 continue;
  474.               }
  475.               }
  476.           }
  477.  
  478.           /* Now that the jump has been tensioned,
  479.              try cross jumping: check for identical code
  480.              before the jump and before its target label. */
  481.  
  482.           /* First, cross jumping of conditional jumps:  */
  483.  
  484.           if (cross_jump && condjump_p (insn))
  485.             {
  486.               rtx newjpos, newlpos;
  487.               rtx x = prev_real_insn (JUMP_LABEL (insn));
  488.  
  489.               /* A conditional jump may be crossjumped
  490.              only if the place it jumps to follows
  491.              an opposing jump that comes back here.  */
  492.  
  493.               if (x != 0 && ! jump_back_p (x, insn))
  494.             /* We have no opposing jump;
  495.                cannot cross jump this insn.  */
  496.             x = 0;
  497.  
  498.               newjpos = 0;
  499.               /* TARGET is nonzero if it is ok to cross jump
  500.              to code before TARGET.  If so, see if matches.  */
  501.               if (x != 0)
  502.             find_cross_jump (insn, x, 2,
  503.                      &newjpos, &newlpos);
  504.  
  505.               if (newjpos != 0)
  506.             {
  507.               do_cross_jump (insn, newjpos, newlpos);
  508.               /* Make the old conditional jump
  509.                  into an unconditional one.  */
  510.               SET_SRC (PATTERN (insn))
  511.                 = gen_rtx (LABEL_REF, VOIDmode, JUMP_LABEL (insn));
  512.               emit_barrier_after (insn);
  513.               changed = 1;
  514.               next = insn;
  515.             }
  516.             }
  517.  
  518.           /* Cross jumping of unconditional jumps:
  519.              a few differences.  */
  520.  
  521.           if (cross_jump && simplejump_p (insn))
  522.             {
  523.               rtx newjpos, newlpos;
  524.               rtx target;
  525.  
  526.               newjpos = 0;
  527.  
  528.               /* TARGET is nonzero if it is ok to cross jump
  529.              to code before TARGET.  If so, see if matches.  */
  530.               find_cross_jump (insn, JUMP_LABEL (insn), 1,
  531.                        &newjpos, &newlpos);
  532.  
  533.               /* If cannot cross jump to code before the label,
  534.              see if we can cross jump to another jump to
  535.              the same label.  */
  536.               /* Try each other jump to this label.  */
  537.               if (INSN_UID (JUMP_LABEL (insn)) < max_uid)
  538.             for (target = jump_chain[INSN_UID (JUMP_LABEL (insn))];
  539.                  target != 0 && newjpos == 0;
  540.                  target = jump_chain[INSN_UID (target)])
  541.               if (target != insn
  542.                   && JUMP_LABEL (target) == JUMP_LABEL (insn)
  543.                   /* Ignore TARGET if it's deleted.  */
  544.                   && ! target->volatil)
  545.                 find_cross_jump (insn, target, 2,
  546.                          &newjpos, &newlpos);
  547.  
  548.               if (newjpos != 0)
  549.             {
  550.               do_cross_jump (insn, newjpos, newlpos);
  551.               changed = 1;
  552.               next = insn;
  553.             }
  554.             }
  555.         }
  556.         }
  557.       else if (GET_CODE (insn) == JUMP_INSN
  558.            && GET_CODE (PATTERN (insn)) == RETURN)
  559.         {
  560.           /* Return insns all "jump to the same place"
  561.          so we can cross-jump between any two of them.  */
  562.           if (cross_jump)
  563.         {
  564.           rtx newjpos, newlpos, target;
  565.  
  566.           newjpos = 0;
  567.  
  568.           /* If cannot cross jump to code before the label,
  569.              see if we can cross jump to another jump to
  570.              the same label.  */
  571.           /* Try each other jump to this label.  */
  572.           for (target = jump_chain[0];
  573.                target != 0 && newjpos == 0;
  574.                target = jump_chain[INSN_UID (target)])
  575.             if (target != insn
  576.             && ! target->volatil
  577.             && GET_CODE (PATTERN (target)) == RETURN)
  578.               find_cross_jump (insn, target, 2,
  579.                        &newjpos, &newlpos);
  580.  
  581.           if (newjpos != 0)
  582.             {
  583.               do_cross_jump (insn, newjpos, newlpos);
  584.               changed = 1;
  585.               next = insn;
  586.             }
  587.         }
  588.         }
  589.  
  590.     }
  591.  
  592.       first = 0;
  593.     }
  594.  
  595.   /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
  596.      If so, delete it, and record that this function can drop off the end.  */
  597.  
  598.   insn = last_insn;
  599.   while (insn && (GET_CODE (insn) == CODE_LABEL
  600.           /* If machine uses explicit RETURN insns, no epilogue,
  601.              then this note precedes the drop-through RETURN.  */
  602.           || (GET_CODE (insn) == JUMP_INSN
  603.               && GET_CODE (PATTERN (insn)) == RETURN)))
  604.     insn = PREV_INSN (insn);
  605.   if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END)
  606.     {
  607.       extern int current_function_returns_null;
  608.       current_function_returns_null = 1;
  609.       delete_insn (insn);
  610.     }
  611. }
  612.  
  613. /* Compare the instructions before insn E1 with those before E2.
  614.    Assume E1 is a jump that jumps to label E2
  615.    (that is not always true but it might as well be).
  616.    Find the longest possible equivalent sequences
  617.    and store the first insns of those sequences into *F1 and *F2.
  618.    Store zero there if no equivalent preceding instructions are found.
  619.  
  620.    We give up if we find a label in stream 1.
  621.    Actually we could transfer that label into stream 2.  */
  622.  
  623. static void
  624. find_cross_jump (e1, e2, minimum, f1, f2)
  625.      rtx e1, e2;
  626.      int minimum;
  627.      rtx *f1, *f2;
  628. {
  629.   register rtx i1 = e1, i2 = e2;
  630.   register rtx p1, p2;
  631.  
  632.   rtx last1 = 0, last2 = 0;
  633.   rtx afterlast1 = 0, afterlast2 = 0;
  634.  
  635.   *f1 = 0;
  636.   *f2 = 0;
  637.  
  638.   while (1)
  639.     {
  640.       i1 = PREV_INSN (i1);
  641.       while (i1 && GET_CODE (i1) == NOTE)
  642.     i1 = PREV_INSN (i1);
  643.  
  644.       i2 = PREV_INSN (i2);
  645.       while (i2 && (GET_CODE (i2) == NOTE || GET_CODE (i2) == CODE_LABEL))
  646.     i2 = PREV_INSN (i2);
  647.  
  648.       if (i1 == 0)
  649.     break;
  650.  
  651.       /* If we will get to this code by jumping, those jumps will be
  652.      tensioned to go directly to the new label (before I2),
  653.      so this cross-jumping won't cost extra.  So reduce the minimum.  */
  654.       if (GET_CODE (i1) == CODE_LABEL)
  655.     {
  656.       --minimum;
  657.       break;
  658.     }
  659.  
  660.       if (i2 == 0 || GET_CODE (i1) != GET_CODE (i2))
  661.     break;
  662.  
  663.       p1 = PATTERN (i1);
  664.       p2 = PATTERN (i2);
  665.     
  666.       if (GET_CODE (p1) != GET_CODE (p2)
  667.       || !rtx_renumbered_equal_p (p1, p2))
  668.     {
  669.       /* Insns fail to match; cross jumping is limited to the following
  670.          insns.  */
  671.  
  672.       /* Don't allow the insn after a compare to be shared by cross-jumping
  673.          unless the compare is also shared.
  674.          Here, if either of these non-matching insns is a compare,
  675.          exclude the following insn from possible cross-jumping.  */
  676.       if ((GET_CODE (p1) == SET && SET_DEST (p1) == cc0_rtx)
  677.           || (GET_CODE (p2) == SET && SET_DEST (p2) == cc0_rtx))
  678.         last1 = afterlast1, last2 = afterlast2, ++minimum;
  679.  
  680.       /* If cross-jumping here will feed a jump-around-jump optimization,
  681.          this jump won't cost extra, so reduce the minimum.  */
  682.       if (GET_CODE (i1) == JUMP_INSN
  683.           && JUMP_LABEL (i1)
  684.           && prev_real_insn (JUMP_LABEL (i1)) == e1)
  685.         --minimum;
  686.       break;
  687.     }
  688.  
  689.       if (GET_CODE (p1) != USE && GET_CODE (p1) != CLOBBER)
  690.     {
  691.       /* Ok, this insn is potentially includable in a cross-jump here.  */
  692.       afterlast1 = last1, afterlast2 = last2;
  693.       last1 = i1, last2 = i2, --minimum;
  694.     }
  695.     }
  696.  
  697.   if (minimum <= 0 && last1 != 0)
  698.     *f1 = last1, *f2 = last2;
  699. }
  700.  
  701. static void
  702. do_cross_jump (insn, newjpos, newlpos)
  703.      rtx insn, newjpos, newlpos;
  704. {
  705.   register rtx label;
  706.   /* Find an existing label at this point
  707.      or make a new one if there is none.  */
  708.   label = PREV_INSN (newlpos);
  709.   if (GET_CODE (label) != CODE_LABEL)
  710.     {
  711.       label = gen_label_rtx ();
  712.       emit_label_after (label, PREV_INSN (newlpos));
  713.       LABEL_NUSES (label) = 0;
  714.     }
  715.   /* Make the same jump insn jump to the new point.  */
  716.   if (GET_CODE (PATTERN (insn)) == RETURN)
  717.     {
  718.       extern rtx gen_jump ();
  719.       PATTERN (insn) = gen_jump (label);
  720.       INSN_CODE (insn) = -1;
  721.       JUMP_LABEL (insn) = label;
  722.       LABEL_NUSES (label)++;
  723.     }
  724.   else
  725.     redirect_jump (insn, label);
  726.   /* Delete the matching insns before the jump.  */
  727.   newjpos = PREV_INSN (newjpos);
  728.   while (NEXT_INSN (newjpos) != insn)
  729.     /* Don't delete line numbers.  */
  730.     if (GET_CODE (NEXT_INSN (newjpos)) != NOTE)
  731.       delete_insn (NEXT_INSN (newjpos));
  732.     else
  733.       newjpos = NEXT_INSN (newjpos);
  734. }
  735.  
  736. /* Return 1 if INSN is a jump that jumps to right after TARGET
  737.    only on the condition that TARGET itself would drop through.
  738.    Assumes that TARGET is a conditional jump.  */
  739.  
  740. static int
  741. jump_back_p (insn, target)
  742.      rtx insn, target;
  743. {
  744.   rtx cinsn, ctarget;
  745.   enum rtx_code codei, codet;
  746.  
  747.   if (simplejump_p (insn) || ! condjump_p (insn)
  748.       || simplejump_p (target))
  749.     return 0;
  750.   if (target != prev_real_insn (JUMP_LABEL (insn)))
  751.     return 0;
  752.  
  753.   cinsn = XEXP (SET_SRC (PATTERN (insn)), 0);
  754.   ctarget = XEXP (SET_SRC (PATTERN (target)), 0);
  755.  
  756.   codei = GET_CODE (cinsn);
  757.   codet = GET_CODE (ctarget);
  758.   if (XEXP (SET_SRC (PATTERN (insn)), 1) == pc_rtx)
  759.     codei = reverse_condition (codei);
  760.   if (XEXP (SET_SRC (PATTERN (target)), 2) == pc_rtx)
  761.     codet = reverse_condition (codet);
  762.   return (codei == codet
  763.       && rtx_renumbered_equal_p (XEXP (cinsn, 0), XEXP (ctarget, 0))
  764.       && rtx_renumbered_equal_p (XEXP (cinsn, 1), XEXP (ctarget, 1)));
  765. }
  766.  
  767. /* Given an rtx-code for a comparison, return the code
  768.    for the negated comparison.  */
  769.  
  770. static enum rtx_code
  771. reverse_condition (code)
  772.      enum rtx_code code;
  773. {
  774.   switch (code)
  775.     {
  776.     case EQ:
  777.       return NE;
  778.  
  779.     case NE:
  780.       return EQ;
  781.  
  782.     case GT:
  783.       return LE;
  784.  
  785.     case GE:
  786.       return LT;
  787.  
  788.     case LT:
  789.       return GE;
  790.  
  791.     case LE:
  792.       return GT;
  793.  
  794.     case GTU:
  795.       return LEU;
  796.  
  797.     case GEU:
  798.       return LTU;
  799.  
  800.     case LTU:
  801.       return GEU;
  802.  
  803.     case LEU:
  804.       return GTU;
  805.  
  806.     default:
  807.       abort ();
  808.       return UNKNOWN;
  809.     }
  810. }
  811.  
  812. /* Return 1 if INSN is an unconditional jump and nothing else.  */
  813.  
  814. int
  815. simplejump_p (insn)
  816.      rtx insn;
  817. {
  818.   register rtx x = PATTERN (insn);
  819.   if (GET_CODE (x) != SET)
  820.     return 0;
  821.   if (GET_CODE (SET_DEST (x)) != PC)
  822.     return 0;
  823.   if (GET_CODE (SET_SRC (x)) != LABEL_REF)
  824.     return 0;
  825.   return 1;
  826. }
  827.  
  828. /* Return nonzero if INSN is a (possibly) conditional jump
  829.    and nothing more.  */
  830.  
  831. static int
  832. condjump_p (insn)
  833.      rtx insn;
  834. {
  835.   register rtx x = PATTERN (insn);
  836.   if (GET_CODE (x) != SET)
  837.     return 0;
  838.   if (GET_CODE (SET_DEST (x)) != PC)
  839.     return 0;
  840.   if (GET_CODE (SET_SRC (x)) == LABEL_REF)
  841.     return 1;
  842.   if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
  843.     return 0;
  844.   if (XEXP (SET_SRC (x), 2) == pc_rtx
  845.       && GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF)
  846.     return 1;
  847.   if (XEXP (SET_SRC (x), 1) == pc_rtx
  848.       && GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF)
  849.     return 1;
  850.   return 0;
  851. }
  852.  
  853. /* Return 1 if in between BEG and END there is no CODE_LABEL insn.  */
  854.  
  855. int
  856. no_labels_between_p (beg, end)
  857.      rtx beg, end;
  858. {
  859.   register rtx p;
  860.   for (p = beg; p != end; p = NEXT_INSN (p))
  861.     if (GET_CODE (p) == CODE_LABEL)
  862.       return 0;
  863.   return 1;
  864. }
  865.  
  866. /* Return the last INSN, CALL_INSN or JUMP_INSN before LABEL;
  867.    or 0, if there is none.  */
  868.  
  869. rtx
  870. prev_real_insn (label)
  871.      rtx label;
  872. {
  873.   register rtx insn = PREV_INSN (label);
  874.   register RTX_CODE code;
  875.  
  876.   while (1)
  877.     {
  878.       if (insn == 0)
  879.     return 0;
  880.       code = GET_CODE (insn);
  881.       if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
  882.     break;
  883.       insn = PREV_INSN (insn);
  884.     }
  885.  
  886.   return insn;
  887. }
  888.  
  889. /* Return the next INSN, CALL_INSN or JUMP_INSN after LABEL;
  890.    or 0, if there is none.  */
  891.  
  892. rtx
  893. next_real_insn (label)
  894.      rtx label;
  895. {
  896.   register rtx insn = NEXT_INSN (label);
  897.   register RTX_CODE code;
  898.  
  899.   while (1)
  900.     {
  901.       if (insn == 0)
  902.     return insn;
  903.       code = GET_CODE (insn);
  904.       if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
  905.     break;
  906.       insn = NEXT_INSN (insn);
  907.     }
  908.  
  909.   return insn;
  910. }
  911.  
  912. /* Return the next CODE_LABEL after the insn INSN, or 0 if there is none.  */
  913.  
  914. rtx
  915. next_label (insn)
  916.      rtx insn;
  917. {
  918.   do insn = NEXT_INSN (insn);
  919.   while (insn != 0 && GET_CODE (insn) != CODE_LABEL);
  920.   return insn;
  921. }
  922.  
  923. /* Follow any unconditional jump at LABEL;
  924.    return the ultimate label reached by any such chain of jumps.
  925.    If LABEL is not followed by a jump, return LABEL.
  926.    If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG.  */
  927.  
  928. static rtx
  929. follow_jumps (label, ignore_loops)
  930.      rtx label;
  931.      int ignore_loops;
  932. {
  933.   register rtx insn;
  934.   register rtx next;
  935.   register rtx value = label;
  936.   register int depth;
  937.  
  938.   for (depth = 0;
  939.        (depth < 10
  940.     && (insn = next_real_insn (value)) != 0
  941.     && GET_CODE (insn) == JUMP_INSN
  942.     && JUMP_LABEL (insn) != 0
  943.     && (next = NEXT_INSN (insn))
  944.     && GET_CODE (next) == BARRIER);
  945.        depth++)
  946.     {
  947.       /* Don't chain through the insn that jumps into a loop
  948.      from outside the loop,
  949.      since that would create multiple loop entry jumps
  950.      and prevent loop optimization.  */
  951.       rtx tem;
  952.       if (!ignore_loops)
  953.     for (tem = value; tem != insn; tem = NEXT_INSN (tem))
  954.       if (GET_CODE (tem) == NOTE
  955.           && NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG)
  956.         return value;
  957.  
  958.       /* If we have found a cycle, make the insn jump to itself.  */
  959.       if (JUMP_LABEL (insn) == label)
  960.     break;
  961.       value = JUMP_LABEL (insn);
  962.     }
  963.   return value;
  964. }
  965.  
  966. /* Assuming that field IDX of X is a vector of label_refs,
  967.    replace each of them by the ultimate label reached by it.
  968.    Return nonzero if a change is made.
  969.    If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG.  */
  970.  
  971. static int
  972. tension_vector_labels (x, idx, ignore_loops)
  973.      register rtx x;
  974.      register int idx;
  975.      int ignore_loops;
  976. {
  977.   int changed = 0;
  978.   register int i;
  979.   for (i = XVECLEN (x, idx) - 1; i >= 0; i--)
  980.     {
  981.       register rtx olabel = XEXP (XVECEXP (x, idx, i), 0);
  982.       register rtx nlabel = follow_jumps (olabel, ignore_loops);
  983.       if (nlabel != olabel)
  984.     {
  985.       XEXP (XVECEXP (x, idx, i), 0) = nlabel;
  986.       ++LABEL_NUSES (nlabel);
  987.       if (--LABEL_NUSES (olabel) == 0)
  988.         delete_insn (olabel);
  989.       changed = 1;
  990.     }
  991.     }
  992.   return changed;
  993. }
  994.  
  995. /* Find all CODE_LABELs referred to in X,
  996.    and increment their use counts.
  997.    Also store one of them in JUMP_LABEL (INSN) if INSN is nonzero.
  998.    Also, when there are consecutive labels,
  999.    canonicalize on the last of them.
  1000.  
  1001.    Note that two labels separated by a loop-beginning note
  1002.    must be kept distinct if we have not yet done loop-optimization,
  1003.    because the gap between them is where loop-optimize
  1004.    will want to move invariant code to.  CROSS_JUMP tells us
  1005.    that loop-optimization is done with.  */
  1006.  
  1007. static void
  1008. mark_jump_label (x, insn, cross_jump)
  1009.      register rtx x;
  1010.      rtx insn;
  1011.      int cross_jump;
  1012. {
  1013.   register RTX_CODE code = GET_CODE (x);
  1014.   register int i;
  1015.   register char *fmt;
  1016.  
  1017.   if (code == LABEL_REF)
  1018.     {
  1019.       register rtx label = XEXP (x, 0);
  1020.       register rtx next;
  1021.       if (GET_CODE (label) != CODE_LABEL)
  1022.     return;
  1023.       /* If there are other labels following this one,
  1024.      replace it with the last of the consecutive labels.  */
  1025.       for (next = NEXT_INSN (label); next; next = NEXT_INSN (next))
  1026.     {
  1027.       if (GET_CODE (next) == CODE_LABEL)
  1028.         label = next;
  1029.       else if (GET_CODE (next) != NOTE
  1030.            || NOTE_LINE_NUMBER (next) == NOTE_INSN_LOOP_BEG
  1031.            || NOTE_LINE_NUMBER (next) == NOTE_INSN_FUNCTION_END)
  1032.         break;
  1033.     }
  1034.       XEXP (x, 0) = label;
  1035.       ++LABEL_NUSES (label);
  1036.       if (insn)
  1037.     JUMP_LABEL (insn) = label;
  1038.       return;
  1039.     }
  1040.  
  1041.   /* Do walk the labels in a vector,
  1042.      but don't set its JUMP_LABEL.  */
  1043.   if (code == ADDR_VEC || code == ADDR_DIFF_VEC)
  1044.     insn = 0;
  1045.  
  1046.   fmt = GET_RTX_FORMAT (code);
  1047.   for (i = GET_RTX_LENGTH (code); i >= 0; i--)
  1048.     {
  1049.       if (fmt[i] == 'e')
  1050.     mark_jump_label (XEXP (x, i), insn, cross_jump);
  1051.       else if (fmt[i] == 'E')
  1052.     {
  1053.       register int j;
  1054.       for (j = 0; j < XVECLEN (x, i); j++)
  1055.         mark_jump_label (XVECEXP (x, i, j), insn, cross_jump);
  1056.     }
  1057.     }
  1058. }
  1059.  
  1060. /* If all INSN does is set the pc, delete it,
  1061.    and delete the insn that set the condition codes for it
  1062.    if that's what the previous thing was.  */
  1063.  
  1064. static void
  1065. delete_jump (insn)
  1066.      rtx insn;
  1067. {
  1068.   register rtx x = PATTERN (insn);
  1069.   register rtx prev;
  1070.  
  1071.   if (GET_CODE (x) == SET
  1072.       && GET_CODE (SET_DEST (x)) == PC)
  1073.     {
  1074.       prev = PREV_INSN (insn);
  1075.       delete_insn (insn);
  1076.       /* We assume that at this stage
  1077.      CC's are always set explicitly
  1078.      and always immediately before the jump that
  1079.      will use them.  So if the previous insn
  1080.      exists to set the CC's, delete it.  */
  1081.       while (prev && GET_CODE (prev) == NOTE)
  1082.     prev = PREV_INSN (prev);
  1083.       if (prev && GET_CODE (prev) == INSN
  1084.       && GET_CODE (PATTERN (prev)) == SET
  1085.       && SET_DEST (PATTERN (prev)) == cc0_rtx)
  1086.     delete_insn (prev);
  1087.     }
  1088. }
  1089.  
  1090. /* Delete insn INSN from the chain of insns and update label ref counts.
  1091.    May delete some following insns as a consequence; may even delete
  1092.    a label elsewhere and insns that follow it.
  1093.  
  1094.    Returns the first insn after INSN that was not deleted.  */
  1095.  
  1096. rtx
  1097. delete_insn (insn)
  1098.      register rtx insn;
  1099. {
  1100.   register rtx next = NEXT_INSN (insn);
  1101.   register rtx prev = PREV_INSN (insn);
  1102.  
  1103.   if (insn->volatil)
  1104.     {
  1105.       /* This insn is already deleted => return first following nondeleted.  */
  1106.       while (next && next->volatil)
  1107.     next = NEXT_INSN (next);
  1108.       return next;
  1109.     }
  1110.  
  1111.   /* Mark this insn as deleted.  */
  1112.  
  1113.   insn->volatil = 1;
  1114.  
  1115.   /* If instruction is followed by a barrier,
  1116.      delete the barrier too.  */
  1117.  
  1118.   if (next != 0 && GET_CODE (next) == BARRIER)
  1119.     {
  1120.       next->volatil = 1;
  1121.       next = NEXT_INSN (next);
  1122.     }
  1123.  
  1124.   /* Patch out INSN (and the barrier if any) */
  1125.  
  1126.   if (optimize)
  1127.     {
  1128.       if (prev)
  1129.     NEXT_INSN (prev) = next;
  1130.  
  1131.       if (next)
  1132.     PREV_INSN (next)= prev;
  1133.     }
  1134.  
  1135.   /* If deleting a jump, decrement the count of the label,
  1136.      and delete the label if it is now unused.  */
  1137.  
  1138.   if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
  1139.     if (--LABEL_NUSES (JUMP_LABEL (insn)) == 0)
  1140.       {
  1141.     /* This can delete NEXT or PREV,
  1142.        either directly if NEXT is JUMP_LABEL (INSN),
  1143.        or indirectly through more levels of jumps.  */
  1144.     delete_insn (JUMP_LABEL (insn));
  1145.     /* I feel a little doubtful about this loop,
  1146.        but I see no clean and sure alternative way
  1147.        to find the first insn after INSN that is not now deleted.
  1148.        I hope this works.  */
  1149.     while (next && next->volatil)
  1150.       next = NEXT_INSN (next);
  1151.     return next;
  1152.       }
  1153.  
  1154.   while (prev && GET_CODE (prev) == NOTE)
  1155.     prev = PREV_INSN (prev);
  1156.  
  1157.   /* If INSN was a label, delete insns following it if now unreachable.  */
  1158.  
  1159.   if (GET_CODE (insn) == CODE_LABEL && prev
  1160.       && GET_CODE (prev) == BARRIER)
  1161.     {
  1162.       register RTX_CODE code;
  1163.       while (next != 0
  1164.          && ((code = GET_CODE (next)) == INSN
  1165.          || code == JUMP_INSN || code == CALL_INSN
  1166.          || code == NOTE))
  1167.     {
  1168.       if (code == NOTE
  1169.           && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
  1170.         next = NEXT_INSN (next);
  1171.       else
  1172.         /* Note: if this deletes a jump, it can cause more
  1173.            deletion of unreachable code, after a different label.
  1174.            As long as the value from this recursive call is correct,
  1175.            this invocation functions correctly.  */
  1176.         next = delete_insn (next);
  1177.     }
  1178.     }
  1179.  
  1180.   return next;
  1181. }
  1182.  
  1183. /* Advance from INSN till reaching something not deleted
  1184.    then return that.  May return INSN itself.  */
  1185.  
  1186. rtx
  1187. next_nondeleted_insn (insn)
  1188.      rtx insn;
  1189. {
  1190.   while (insn->volatil)
  1191.     insn = NEXT_INSN (insn);
  1192.   return insn;
  1193. }
  1194.  
  1195. /* Invert the condition of the jump JUMP, and make it jump
  1196.    to label NLABEL instead of where it jumps now.  */
  1197.  
  1198. void
  1199. invert_jump (jump, nlabel)
  1200.      rtx jump, nlabel;
  1201. {
  1202.   register rtx olabel = JUMP_LABEL (jump);
  1203.   invert_exp (PATTERN (jump), olabel, nlabel);
  1204.   JUMP_LABEL (jump) = nlabel;
  1205.   ++LABEL_NUSES (nlabel);
  1206.   INSN_CODE (jump) = -1;
  1207.  
  1208.   if (--LABEL_NUSES (olabel) == 0)
  1209.     delete_insn (olabel);
  1210. }
  1211.  
  1212. /* Invert the jump condition of rtx X,
  1213.    and replace OLABEL with NLABEL throughout.  */
  1214.  
  1215. static void
  1216. invert_exp (x, olabel, nlabel)
  1217.      rtx x;
  1218.      rtx olabel, nlabel;
  1219. {
  1220.   register RTX_CODE code = GET_CODE (x);
  1221.   register int i;
  1222.   register char *fmt;
  1223.  
  1224.   if (code == IF_THEN_ELSE)
  1225.     {
  1226.       /* Inverting the jump condition of an IF_THEN_ELSE
  1227.      means exchanging the THEN-part with the ELSE-part.  */
  1228.       register rtx tem = XEXP (x, 1);
  1229.       XEXP (x, 1) = XEXP (x, 2);
  1230.       XEXP (x, 2) = tem;
  1231.     }
  1232.  
  1233.   if (code == LABEL_REF)
  1234.     {
  1235.       if (XEXP (x, 0) == olabel)
  1236.     XEXP (x, 0) = nlabel;
  1237.       return;
  1238.     }
  1239.  
  1240.   fmt = GET_RTX_FORMAT (code);
  1241.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1242.     {
  1243.       if (fmt[i] == 'e')
  1244.     invert_exp (XEXP (x, i), olabel, nlabel);
  1245.       if (fmt[i] == 'E')
  1246.     {
  1247.       register int j;
  1248.       for (j = 0; j < XVECLEN (x, i); j++)
  1249.         invert_exp (XVECEXP (x, i, j), olabel, nlabel);
  1250.     }
  1251.     }
  1252. }
  1253.  
  1254. /* Make jump JUMP jump to label NLABEL instead of where it jumps now.
  1255.    If the old jump target label is unused as a result,
  1256.    it and the code following it may be deleted.  */
  1257.  
  1258. void
  1259. redirect_jump (jump, nlabel)
  1260.      rtx jump, nlabel;
  1261. {
  1262.   register rtx olabel = JUMP_LABEL (jump);
  1263.  
  1264.   if (nlabel == olabel)
  1265.     return;
  1266.  
  1267.   redirect_exp (PATTERN (jump), olabel, nlabel);
  1268.   JUMP_LABEL (jump) = nlabel;
  1269.   ++LABEL_NUSES (nlabel);
  1270.   INSN_CODE (jump) = -1;
  1271.  
  1272.   if (--LABEL_NUSES (olabel) == 0)
  1273.     delete_insn (olabel);
  1274. }
  1275.  
  1276. /* Throughout the rtx X,
  1277.    alter (LABEL_REF OLABEL) to (LABEL_REF NLABEL).  */
  1278.  
  1279. static void
  1280. redirect_exp (x, olabel, nlabel)
  1281.      rtx x;
  1282.      rtx olabel, nlabel;
  1283. {
  1284.   register RTX_CODE code = GET_CODE (x);
  1285.   register int i;
  1286.   register char *fmt;
  1287.  
  1288.   if (code == LABEL_REF)
  1289.     {
  1290.       if (XEXP (x, 0) == olabel)
  1291.     XEXP (x, 0) = nlabel;
  1292.       return;
  1293.     }
  1294.  
  1295.   fmt = GET_RTX_FORMAT (code);
  1296.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1297.     {
  1298.       if (fmt[i] == 'e')
  1299.     redirect_exp (XEXP (x, i), olabel, nlabel);
  1300.       if (fmt[i] == 'E')
  1301.     {
  1302.       register int j;
  1303.       for (j = 0; j < XVECLEN (x, i); j++)
  1304.         redirect_exp (XVECEXP (x, i, j), olabel, nlabel);
  1305.     }
  1306.     }
  1307. }
  1308.  
  1309. /* Like rtx_equal_p except that it considers two REGs as equal
  1310.    if they renumber to the same value.  */
  1311.  
  1312. int
  1313. rtx_renumbered_equal_p (x, y)
  1314.      rtx x, y;
  1315. {
  1316.   register int i;
  1317.   register RTX_CODE code = GET_CODE (x);
  1318.   register char *fmt;
  1319.       
  1320.   if (x == y)
  1321.     return 1;
  1322.   if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
  1323.       && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
  1324.                   && GET_CODE (SUBREG_REG (y)) == REG)))
  1325.     {
  1326.       register int j;
  1327.  
  1328.       if (GET_MODE (x) != GET_MODE (y))
  1329.     return 0;
  1330.  
  1331.       if (code == SUBREG)
  1332.     {
  1333.       i = REGNO (SUBREG_REG (x));
  1334.       if (reg_renumber[i] >= 0)
  1335.         i = reg_renumber[i];
  1336.       i += SUBREG_WORD (x);
  1337.     }
  1338.       else
  1339.     {
  1340.       i = REGNO (x);
  1341.       if (reg_renumber[i] >= 0)
  1342.         i = reg_renumber[i];
  1343.     }
  1344.       if (GET_CODE (y) == SUBREG)
  1345.     {
  1346.       j = REGNO (SUBREG_REG (y));
  1347.       if (reg_renumber[j] >= 0)
  1348.         j = reg_renumber[j];
  1349.       j += SUBREG_WORD (y);
  1350.     }
  1351.       else
  1352.     {
  1353.       j = REGNO (y);
  1354.       if (reg_renumber[j] >= 0)
  1355.         j = reg_renumber[j];
  1356.     }
  1357.       return i == j;
  1358.     }
  1359.   /* Now we have disposed of all the cases 
  1360.      in which different rtx codes can match.  */
  1361.   if (code != GET_CODE (y))
  1362.     return 0;
  1363.   switch (code)
  1364.     {
  1365.     case PC:
  1366.     case CC0:
  1367.     case ADDR_VEC:
  1368.     case ADDR_DIFF_VEC:
  1369.       return 0;
  1370.  
  1371.     case CONST_INT:
  1372.       return XINT (x, 0) == XINT (y, 0);
  1373.  
  1374.     case LABEL_REF:
  1375.       /* Two label-refs are equivalent if they point at labels
  1376.      in the same position in the instruction stream.  */
  1377.       return (next_real_insn (XEXP (x, 0))
  1378.           == next_real_insn (XEXP (y, 0)));
  1379.  
  1380.     case SYMBOL_REF:
  1381.       return XSTR (x, 0) == XSTR (y, 0);
  1382.     }
  1383.  
  1384.   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
  1385.  
  1386.   if (GET_MODE (x) != GET_MODE (y))
  1387.     return 0;
  1388.  
  1389.   /* Compare the elements.  If any pair of corresponding elements
  1390.      fail to match, return 0 for the whole things.  */
  1391.  
  1392.   fmt = GET_RTX_FORMAT (code);
  1393.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1394.     {
  1395.       register int j;
  1396.       switch (fmt[i])
  1397.     {
  1398.     case 'i':
  1399.       if (XINT (x, i) != XINT (y, i))
  1400.         return 0;
  1401.       break;
  1402.  
  1403.     case 's':
  1404.       if (strcmp (XSTR (x, i), XSTR (y, i)))
  1405.         return 0;
  1406.       break;
  1407.  
  1408.     case 'e':
  1409.       if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
  1410.         return 0;
  1411.       break;
  1412.  
  1413.     case '0':
  1414.       break;
  1415.  
  1416.     case 'E':
  1417.       if (XVECLEN (x, i) != XVECLEN (y, i))
  1418.         return 0;
  1419.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  1420.         if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
  1421.           return 0;
  1422.       break;
  1423.  
  1424.     default:
  1425.       abort ();
  1426.     }
  1427.     }
  1428.   return 1;
  1429. }
  1430.  
  1431. /* If X is a hard register or equivalent to one or a subregister of one,
  1432.    return the hard register number.  Otherwise, return -1.
  1433.    Any rtx is valid for X.  */
  1434.  
  1435. int
  1436. true_regnum (x)
  1437.      rtx x;
  1438. {
  1439.   if (GET_CODE (x) == REG)
  1440.     {
  1441.       if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
  1442.     return reg_renumber[REGNO (x)];
  1443.       return REGNO (x);
  1444.     }
  1445.   if (GET_CODE (x) == SUBREG)
  1446.     {
  1447.       int base = true_regnum (SUBREG_REG (x));
  1448.       if (base >= 0)
  1449.     return SUBREG_WORD (x) + base;
  1450.     }
  1451.   return -1;
  1452. }
  1453.